package com.nd.android.musicboxlibrary.service.notification;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;
import android.widget.RemoteViews;

import com.nd.android.musicboxlibrary.R;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.business.comm.MagicNumber;
import com.nd.android.musicboxlibrary.service.player.MusicService;
import com.nd.android.musicboxlibrary.ui.player.PlayerActivity;
import com.nd.android.musicboxlibrary.util.MusicBoxUtils;

/**
 * 音乐通知 Created 2014-7-14 下午10:04:12
 *
 * @version
 * @author zxq
 * @see
 */
public class MusicNotification extends Notification implements MagicNumber {

    /**
     *
     * <br>Created 2014年11月28日 上午12:05:46
     * @author       wat.ztag
     */
    @Override
    public void initMagicNumber() {
    }

    /**
     * Creates a new instance of MusicNotification. Created 2014年9月15日 下午11:15:16
     *
     * @param context
     *            上下文
     * @param isPlaying
     *            是否播放中
     * @param musicInfo
     *            音乐
     * @param bitmap
     *            图片
     */
    @SuppressWarnings("deprecation")
    public MusicNotification(final Context context, final boolean isPlaying,
                             final MusicInfo musicInfo, Bitmap bitmap) {
        this.icon = R.drawable.mbx_music_notify;
        this.tickerText = musicInfo.getMusicName();
        this.when = System.currentTimeMillis();
        this.defaults = Notification.DEFAULT_LIGHTS;
        // 永久驻留
        this.flags = FLAG_ONGOING_EVENT | FLAG_NO_CLEAR;
        final RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.mbx_player_notify_view);
        remoteViews.setTextViewText(R.id.tv_notify_music_name, musicInfo.getMusicName());
        // BUG58414要求更改为显示歌手名
        remoteViews.setTextViewText(R.id.tv_notify_special_name, musicInfo.getSingerName());
        remoteViews.setImageViewResource(R.id.iv_notify_music, R.drawable.mbx_main_music_default);
        if (bitmap != null) {
            remoteViews.setImageViewBitmap(R.id.iv_notify_music, bitmap);
        }

        // 3.0以下不显示按钮
        if (Build.VERSION.SDK_INT < n11) {
            remoteViews.setViewVisibility(R.id.iv_notify_pause, View.GONE);
            remoteViews.setViewVisibility(R.id.iv_notify_play, View.GONE);
            remoteViews.setViewVisibility(R.id.iv_notify_next, View.GONE);
            remoteViews.setViewVisibility(R.id.iv_notify_prev, View.GONE);
        } else {
            // 播放中，显示暂停按钮
            if (isPlaying) {
                remoteViews.setViewVisibility(R.id.iv_notify_pause, View.VISIBLE);
                remoteViews.setViewVisibility(R.id.iv_notify_play, View.GONE);
                Intent intent = MusicBoxUtils.getMusicService(context);
                intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PAUSE);
                PendingIntent pauseIntent = PendingIntent.getService(context, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.iv_notify_pause, pauseIntent);
            } else {
                // 不在播放状态，显示播放按钮
                remoteViews.setViewVisibility(R.id.iv_notify_pause, View.GONE);
                remoteViews.setViewVisibility(R.id.iv_notify_play, View.VISIBLE);
                Intent intent = MusicBoxUtils.getMusicService(context);
                intent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PLAY);
                PendingIntent playIntent = PendingIntent.getService(context, 1, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                remoteViews.setOnClickPendingIntent(R.id.iv_notify_play, playIntent);
            }
            // 上一首
            Intent prevIntent = MusicBoxUtils.getMusicService(context);
            prevIntent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_PREV);
            PendingIntent prevPendingIntent = PendingIntent.getService(context, 2, prevIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.iv_notify_prev, prevPendingIntent);
            // 下一首
            Intent nextIntent = MusicBoxUtils.getMusicService(context);
            nextIntent.putExtra(MusicService.PLAYER_ACTION, MusicService.ACTION_NEXT);
            PendingIntent nextPendingIntent = PendingIntent.getService(context, n3, nextIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.iv_notify_next, nextPendingIntent);
        }
        // 跳转播放界面
        Intent intent = new Intent(context, PlayerActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, n4, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        setLatestEventInfo(context, musicInfo.getSingerName(), musicInfo.getMusicName(), contentIntent);
        this.contentIntent = contentIntent;
        this.contentView = remoteViews;
    }
}
